package com.itheima.mall.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.mapper.ProductCategoryMapper;
import com.itheima.mall.pojo.ProductCategory;
import com.itheima.mall.service.PmsProductCategoryService;
import com.itheima.mall.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PmsProductCategoryServiceImpl implements PmsProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    //添加商品分类
    @Override
    public void createProductCategory(ProductCategory pmsProductCategoryParam) {
        productCategoryMapper.insert(pmsProductCategoryParam);
    }
    //根据id删除商品分类
    @Override
    public void deleteProductCategoryById(Integer id) {
        productCategoryMapper.deleteById(id);
    }
    //查询所有一级分类及子分类
    @Override
    public List<ProductCategory> listWithChildren() {
        //调价构造器查询一级分类集合
        LambdaQueryWrapper<ProductCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProductCategory::getLevel , 0);
        List<ProductCategory> categoryList = productCategoryMapper.selectList(lambdaQueryWrapper);
        for (ProductCategory productCategory : categoryList) {
            //查询一级分类id ,根据id查询子类
            Long id = productCategory.getId();
            LambdaQueryWrapper<ProductCategory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProductCategory::getParentId , id);
            List<ProductCategory> childrenList = productCategoryMapper.selectList(queryWrapper);
            productCategory.setChildren(childrenList);
        }

        return categoryList;
    }
    //分页查询商品分类
    @Override
    public PageResult selectPageByParentId(Integer parentId, Integer pageNum, Integer pageSize) {
        //分页构造器
        Page<ProductCategory> page = new Page<>(pageNum , pageSize);
        //条件构造 根据parentId 查询商品分类
        LambdaQueryWrapper<ProductCategory> lambdaQueryWrapper= new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProductCategory::getParentId , parentId);


        Page<ProductCategory> pageInfo = productCategoryMapper.selectPage(page, lambdaQueryWrapper);
        return new PageResult(pageNum , pageSize , (int) pageInfo.getTotal() , pageInfo.getRecords());
    }
    //批量修改导航栏状态
    @Override
    public void updateNavStatusByIds(Integer ids, Integer navStatus) {
        //条件构造器
        LambdaQueryWrapper<ProductCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(ProductCategory::getId , ids);

        ProductCategory productCategory = new ProductCategory();
        productCategory.setNavStatus(Long.valueOf(navStatus));
        productCategoryMapper.update(productCategory , lambdaQueryWrapper);

    }
    //修改显示状态
    @Override
    public void updateShowStatus(Integer ids, Integer showStatus) {
        LambdaQueryWrapper<ProductCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(ProductCategory::getId , ids);

        ProductCategory productCategory = new ProductCategory();
        productCategory.setShowStatus(Long.valueOf(showStatus));
        productCategoryMapper.update(productCategory , lambdaQueryWrapper);
    }
    //根据id 更新商品
    @Override
    public void updateProductCategoryById(Integer id , ProductCategory productCategoryParam) {
        productCategoryParam.setId(Long.valueOf(id));
        productCategoryMapper.updateById(productCategoryParam);
    }
    //根据id查询商品分类
    @Override
    public ProductCategory selectProductCategoryById(Integer id) {
        ProductCategory productCategory = productCategoryMapper.selectById(id);
        return productCategory;
    }
    //修改商品分类

}
